package com.avic.rong.adapter.show;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.api.focus_fans.FocusStateApi;
import com.avic.rong.bean.show_bean.TypeBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 关注
 * Creater: Created by wangyz on 2017/1/10.
 * Email: 936804097@qq.com
 */

public class TypethreeAdapter extends RecyclerView.Adapter<TypethreeAdapter.RecommendViewHolder> {


    private Context mContext;
    private List<TypeBean> data = new ArrayList<>();


    public TypethreeAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<TypeBean> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    @Override
    public RecommendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.type_3_layout, parent, false);

        return new RecommendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecommendViewHolder holder, final int position) {

        TypeBean datas = data.get(position);
        Glide.with(mContext).load(datas.getLogo())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .error(R.mipmap.deafult_image_header)
                .into(holder.imageIcon);
        holder.naemType3.setText(datas.getUser_name());
        holder.neirongType3.setText(datas.getUser_desc());
        boolean follow = datas.isFollow();
        if (follow){
            holder.guanzhuType3.setText("已关注");
        }else{
            holder.guanzhuType3.setText("+ 关注");
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(mContext, ArticleDetailActivity.class);
//                intent.putExtra("contId", data.get(position).getContId() + "");
//                mContext.startActivity(intent);
            }
        });

        holder.guanzhuType3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean follow = datas.isFollow();
                datas.setFollow(!follow);
                FocusStateApi focusStateApi = new FocusStateApi((MainActivity)mContext, httpOnNextListener);
                String logintoken = SharedPreferencesUtil.getInstance(mContext).getLogintoken();
                focusStateApi.setArgument(datas.getUser_id(), logintoken);
                focusStateApi.setCache(false);
                HttpManager.getInstance().doHttpDeal(focusStateApi);
            }
        });


    }
    HttpOnNextListener<BaseResultEntity> httpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {

            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_200)) {
                    notifyDataSetChanged();
                Toast.makeText(mContext,"关注成功",Toast.LENGTH_SHORT).show();

            }else{
                Toast.makeText(mContext,baseResultEntity.getMsg(),Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public int getItemCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }


    class RecommendViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_icon)
        ImageView imageIcon;
        @BindView(R.id.naem_type3)
        TextView naemType3;
        @BindView(R.id.neirong_type3)
        TextView neirongType3;
        @BindView(R.id.guanzhu_type3)
        TextView guanzhuType3;

        public RecommendViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }



}
